﻿using System;
using System.Collections.Generic;
using System.Web;
using ChapterX.Common;
using ChapterX.Data;
using ChapterX.Data.AccessStatisticsDSTableAdapters;

namespace ChapterX.BusinessFacade
{
    public class StatisticSystem
    {
        protected static int BaseCount
        {
            get
            {
                return ChapterXConfiguration.BaseCount;
            }
        }

        public static int Count
        {
            get
            {
                int count = BaseCount;

                try
                {
                    AccessStatisticsTableAdapter adapter = new AccessStatisticsTableAdapter();
                    count += (int)adapter.GetAccessCount();
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                }

                return count;
            }
        }

        public static void LogAccess(String ip, String userAgent)
        {
            try
            {
                AccessStatisticsTableAdapter adapter = new AccessStatisticsTableAdapter();
                adapter.LogAccess(ip, Utils.HasValue(userAgent) ? Utils.GetValue(userAgent) : "N/A");
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
        }
    }
}